<?php

class DocVideosAction extends Action {
  private $_dev = 0;
  function _initialize() {
    $this->_dev = C("DEV_FLAG");
    $this->assign("istp", 1);//circle-header2中有TP和PHPWIND都用到，加上这个参数进行区别
  }

  public function index() {
    $m = M();
    $sql = "select * from oj_admin_video where `check` = 1 order by datetime desc limit 1";
    $zy = array(1=>"综合管理类",2=>"环境保护类",3=>"安全生产类",4=>"劳动卫生类"/*,5=>"技术规范与标准类",6=>"产品标准类",7=>"国外法规类"*/);
    $video = $m->query($sql);
    $video = $video[0];
    $this->assign('video', $video);
    $this->assign("zy", $zy);
    //相关视频
    $sql = "select * from oj_admin_video where `check` = 1 order by datetime desc limit 1,6";
    $relateList = $m->query($sql);
    $this->assign('relateList', $relateList);
    //专业相关列表
    $zyList = array();
    foreach ($zy as $i => $l) {
      $sql = "select * from oj_admin_video where zy_id in (select id from a_zy where did = $i or id = $i) and `check` = 1 order by datetime desc limit 0,10";
      $zyList[$i] = $m->query($sql);
    }
    $this->assign('zyList', $zyList);
    //按照年，月，日视频排行
    $rank = $this->_getVideoRankList();
    $this->assign("rank", $rank);
    $this->display();
  }

  public function videos() {
//    if ($_GET["zy_id"]) {
//      $zyID = $_GET['zy_id'];
//      $where = "`check` = 1 and zy_id in (select id from a_zy where did = $zyID or id = $zyID)";
//    }else $where = " check = 1";
    $m = M('OjAdminVideo');
    import("@.ORG.Page"); //导入分页类
    $records = 20;//每页多少条记录
    $where = "`check` = 1";
    $count = $m->where($where)->count();
    $p = new Page ($count,$records);
    $p->setConfig('header','篇记录');
    $p->setConfig('prev',"上一页");
    $p->setConfig('next','下一页');
    $p->setConfig('first','<<');
    $p->setConfig('last','>>');
    $page = $p->show ();
    $limit = " limit ".$p->firstRow.",".$p->listRows;
    $list = $m->where($where.$limit)->select();
    $this->assign("count", $count);
    $this->assign("list", $list);
    $this->assign("nowPage",$p->nowPage);
    $this->assign("page", $page );
    $this->assign("records",$records);
    $this->display();
  }

  //获取排行列表，按照年月日
  //暂时按照最新和热门进行排行
  private function _getVideoRankList() {
    $rt = array();
    $m = M("OjAdminVideo");
    $rt["newest"] = $m->order("datetime desc")->limit("0,10")->select();
    $rt["hot"] = $m->order("read_num desc")->limit("0,10")->select();
    return $rt;
//    $year = time() - (int)3600*24*365;
//    $month = time() - (int)3600*24*30;
//    $day = time() - (int)3600*24*1;
//    $order  = "read_num desc ";
//    $limit = "0,10";
//    $rt["year"] = $m->where("datetime > $year")->order($order)->limit($limit)->select();
//    $rt["month"] = $m->where("datetime > $month")->order($order)->limit($limit)->select();
//    $rt["day"] = $m->where("datetime > $day")->order($order)->limit($limit)->select();
  }

  //视频内容页面
  public function video() {
    $id = $_GET["id"];
    $m = M("OjAdminVideo");
    $result = $m->where("vid = $id")->find();
    //增加浏览次数
    $m->query("update oj_admin_video set read_num = read_num + 1 where vid = $id");
    if ($result) {
      //相关视频
      $sql = "select * from oj_admin_video where `check` = 1 and zy_id = ".$result['zy_id']." order by datetime desc limit 1,6";
      $relateList = $m->query($sql);
      $this->assign('relate_list', $relateList);
      $this->assign("video", $result);
      $this->display();
    }else $this->error("你浏览的视频不存在");
  }
}
?>
